package com.yipao.db.service.goods;

import com.yipao.db.dao.goods.GoodsProductMapper;
import com.yipao.db.dao.order.YipaoGoodsProductMapper;
import com.yipao.db.domain.goods.YipaoGoodsProduct;
import com.yipao.db.domain.YipaoGoodsProductExample;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class GoodsProductService {
    @Resource
    private GoodsProductMapper goodsProductMapper;
    @Resource
    private YipaoGoodsProductMapper yipaoGoodsProductMapper;

    public List<YipaoGoodsProduct> queryByGid(Integer gid) {
        YipaoGoodsProductExample example = new YipaoGoodsProductExample();
        example.or().andGoodsIdEqualTo(gid).andDeletedEqualTo(false);
        return yipaoGoodsProductMapper.selectByExample(example);
    }

    public YipaoGoodsProduct findById(Integer id) {
        return yipaoGoodsProductMapper.selectByPrimaryKey(id);
    }

    public void deleteById(Integer id) {
        yipaoGoodsProductMapper.logicalDeleteByPrimaryKey(id);
    }

    public void add(YipaoGoodsProduct goodsProduct) {
        goodsProduct.setAddTime(LocalDateTime.now());
        goodsProduct.setUpdateTime(LocalDateTime.now());
        yipaoGoodsProductMapper.insertSelective(goodsProduct);
    }

    public int count() {
        YipaoGoodsProductExample example = new YipaoGoodsProductExample();
        example.or().andDeletedEqualTo(false);
        return (int) yipaoGoodsProductMapper.countByExample(example);
    }

    public void deleteByGid(Integer gid) {
        YipaoGoodsProductExample example = new YipaoGoodsProductExample();
        example.or().andGoodsIdEqualTo(gid);
        yipaoGoodsProductMapper.logicalDeleteByExample(example);
    }

    public int addStock(Integer id, Short num) {
        return goodsProductMapper.addStock(id, num);
    }

    public int reduceStock(Integer id, Short num) {
        return goodsProductMapper.reduceStock(id, num);
    }

    /**
     * 根据id修改商品
     *
     * @param product
     */
    public void updateById(YipaoGoodsProduct product) {
        product.setUpdateTime(LocalDateTime.now());
        yipaoGoodsProductMapper.updateByPrimaryKeySelective(product);
    }
}
